package com.omybug.study.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.omybug.study.entity.Book;
import com.omybug.study.entity.JsonResponse;
import com.omybug.study.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author oMyBug
 * @since 2018-09-04
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping
    public JsonResponse list(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size){
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.orderByDesc("id");
        IPage<Book> books = bookService.page(new Page<Book>(page, size), bookQueryWrapper);
        return JsonResponse.success(books);
    }

    @GetMapping("/search")
    public JsonResponse search(@RequestParam(value = "keyword", required = false) String keyword,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size){
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.orderByDesc("id");
        if(null != keyword){
            bookQueryWrapper.like("name", "%"+keyword+"%");
        }
        IPage<Book> books = bookService.page(new Page<Book>(page, size), bookQueryWrapper);
        return JsonResponse.success(books);
    }

    @GetMapping("/{bookId}")
    public JsonResponse index(@PathVariable("bookId") Integer bookId){
        Book book = new Book();
        book = book.selectById(bookId);
        if(null == book){
            return JsonResponse.fail("Book不存在");
        }
        return JsonResponse.success(book);
    }

    @PostMapping
    public JsonResponse insert(@Validated(Book.Create.class) Book book){
        if(book.insert()){
            return JsonResponse.success();
        }
        return JsonResponse.fail("添加失败！");
    }

    @PutMapping
    public JsonResponse save(@Validated(Book.Update.class) Book book){
        if(book.updateById()){
            return JsonResponse.success();
        }
        return JsonResponse.fail("保存失败");
    }


    @DeleteMapping("/{bookId}")
    public JsonResponse delete(@PathVariable("bookId") Integer bookId){
        // 方法1: AR删除
//        Book book = new Book();
//        book.setId(bookId);
//        if(book.deleteById()){
//            return JsonResponse.success();
//        }

        // 方法2: 条件删除
        UpdateWrapper<Book> updateWrapper = new UpdateWrapper<Book>();
        updateWrapper.eq("id", bookId);
        if(new Book().delete(updateWrapper)){
            return JsonResponse.success();
        }

        return JsonResponse.fail("删除失败");
    }

}

